package com.hhh.demo.sx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.demo.sx.bean.Shop;
import com.hhh.demo.sx.bean.User;
import com.hhh.demo.sx.mapper.ShopMapper;
import com.hhh.demo.sx.mapper.UserMapper;
import com.hhh.demo.sx.service.ShopService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhh.demo.sx.util.ResultPage;
import com.hhh.demo.sx.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hhh
 * @since 2022-01-04
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper;


    @Override
    public Map<String, Object> findAll(int p, int limit, String condition) {

        User user = SessionUtil.getCurrentUser();
        String username=user.getUsername();


        IPage<Shop> page = null;
        QueryWrapper<Shop> warpper = new QueryWrapper<>();
        Page<Shop> pages = new Page<>(p, limit);
        JSONObject jsonObject = JSONObject.parseObject(condition);
        warpper.eq(!username.equals(""),"createby",username);
        if (jsonObject != null) {
            String name = (String) jsonObject.get("name");
            warpper.like(!name.equals(""), "name", name);


        }
        page= shopMapper.selectPage(pages,warpper);
        return ResultPage.Page(page.getRecords(), page.getTotal());
    }
}
